//
//  WBBPopAlertView.m
//  UILbrary
//
//  Created by bin on 2024/11/19.
//  Copyright © 2024 mac. All rights reserved.
//

#import "WBBPopAlertView.h"
#import <Masonry/Masonry.h>

// 添加宏定义
#define kTitleFont [UIFont boldSystemFontOfSize:20]
#define kContentFont [UIFont systemFontOfSize:16]
#define kContentColor [UIColor darkGrayColor]
#define kConfirmButtonColor [UIColor systemBlueColor]

@interface WBBPopAlertView()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *confirmButton;

@end

@implementation WBBPopAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor clearColor];
    
    self.containerView = [[UIView alloc] init];
    self.containerView.backgroundColor = [UIColor whiteColor];
    self.containerView.layer.cornerRadius = 10;
    self.containerView.layer.masksToBounds = YES;
    [self addSubview:self.containerView];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = kTitleFont;
    self.titleLabel.text = @"标题";
    [self.containerView addSubview:self.titleLabel];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.textAlignment = NSTextAlignmentCenter;
    self.contentLabel.font = kContentFont;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.text = @"弹窗内容";
    self.contentLabel.textColor = kContentColor;
    self.contentLabel.backgroundColor = [UIColor systemGrayColor];
    [self.containerView addSubview:self.contentLabel];
    
    self.confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    self.confirmButton.backgroundColor = [UIColor systemBlueColor];
    [self.confirmButton setTitleColor:kConfirmButtonColor forState:UIControlStateNormal];
    [self.containerView addSubview:self.confirmButton];
    
    // 添加按钮事件
    [self.confirmButton addTarget:self action:@selector(confirmButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.superview);
    }];
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_greaterThanOrEqualTo(280);
        make.width.lessThanOrEqualTo(self).multipliedBy(0.8);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containerView).offset(20);
        make.left.equalTo(self.containerView).offset(20);
        make.right.equalTo(self.containerView).offset(-20);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
        make.left.equalTo(self.containerView).offset(20);
        make.right.equalTo(self.containerView).offset(-20);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(30);
        make.left.equalTo(self.containerView).offset(20);
        make.right.equalTo(self.containerView).offset(-20);
        make.height.mas_equalTo(44);
        make.bottom.equalTo(self.containerView).offset(-20);
    }];
}


- (void)setTitle:(NSString *)title content:(NSString *)content {
    self.titleLabel.text = title;
    self.contentLabel.text = content;
}

#pragma mark - Button Actions
- (void)confirmButtonClicked {
    if (self.confirmCallback) {
        self.confirmCallback();
    }
}

@end
